Ext.define('wmf.controller.keyword.keyword', {
    extend: 'Ext.app.Controller',
    stores: ['keyword.keyword',
             'keyword.query'],
    models: ['keyword.keyword'],
    views: ['keyword.main', 
            'keyword.List', 
            'keyword.Edit',
            'keyword.query',
            'keyword.east',
			'keyword.queryData'
            ],
    refs: [{
        ref: 'keywordList',
        selector: 'keywordList'
    },{
        ref: 'keywordEast',
        selector: 'keywordEast'
	},{
        ref: 'keywordTree',
        selector: 'keywordTree'
	},{
        ref: 'keywordMain',
        selector: 'keywordMain'
    },{
        ref: 'keywordEdit',
        selector: 'keywordEdit'
    },{
    	ref: 'keywordQuery',
    	selector: 'keywordQuery'
    },{
    	ref: 'keywordExport',
    	selector: 'keywordExport'
    },{
    	ref: 'keywordQueryData',
    	selector: 'keywordQueryData'
    }],
    
    init: function () {
        this.control({
        	'keywordList' : {
			    beforerender : this.onBeforeRender
			},
			'keywordTree' : {
				itemclick: this.menuClick
			},
			'keywordEast tool#minimize': {
				click: this.onMinToolClick
			},
        	'keywordQuery button#save': {//查询页面下的保存按钮操作
				click: this.onQuerySaveButtonClick
			},
			'keywordQuery button#cancel': {//查询页面下的取消按钮操作
				click: this.onMinToolClick
			},
			'keywordQuery button#query': {//查询页面下的查询按钮操作
				click: this.onQueryQueryButtonClick
			},
			'keywordEdit button#cancel': {//编辑页面下的取消按钮操作
				click: this.onMinToolClick
			},
			'keywordEdit button#submitKeyword': {//编辑页面下的提交关键字按钮操作
				click: this.onSubmitKeywordClick
			},
			'keywordEdit button#submitFile': {//编辑页面下的提交文件按钮操作
				click: this.onSubmitFileClick
			},
			'keywordEdit button#reason': {//编辑页面下的操作原因按钮操作
				click: this.onReasonClick
			},
			'keywordExport button#cancel': {//导出页面下的取消按钮操作
				click: this.onMinToolClick
			},
			'keywordExport button#cancel': {//导出页面下的导出按钮操作
				click: this.onExportExportButtonClick
			},
			'keywordList button#query': {//关键字主页面的查询按钮操作
                click: this.onQueryButtonClick
            },
			'keywordList button#edit': {//关键字主页面的编辑按钮操作
                click: this.onEditButtonClick
            },
            'keywordList button#delete': {//关键字主页面的删除按钮操作
                click: this.onRemoveButtonClick
            },
            'keywordList button#export': {//关键字主页面的导出按钮操作
                click: this.onExportButtonClick
            }
        });
        wmf.keyword = {};
        wmf.keyword.params = {};
        wmf.keyword.data = {};
        wmf.keyword.init = true;
    },

	//关键字主页面的查询按钮操作
    onQueryButtonClick: function(){
        var win = this.getKeywordEast();
        win.removeAll();
        win.add(Ext.widget("keywordQuery"));
        win.width = "40%";
        win.hide();
        win.show();
        wmf.keyword.params = {};
        this.getKeywordQueryData().getStore().loadData([]);
    },
	
	//关键字主页面的编辑按钮操作
	onEditButtonClick: function(){
        var win = this.getKeywordEast();
        win.removeAll();
        win.add(Ext.widget("keywordEdit"));
		win.width = "40%";
        win.hide();
        win.show();
    },
	
	//关键字主页面的导出按钮操作
    onExportButtonClick: function(){
		window.location.href='keyword/exportKeywordList.do';
    },
	
	//查询页面下的查询按钮操作
	onQueryQueryButtonClick: function(){
    	var me = this;
    	var former = this.getKeywordQuery();
    	var value = former.getValues();
		wmf.keyword.init = true;
		wmf.keyword.params.keyword = value.keyword;
		me.getKeywordList().getStore().pagebar.getStore().currentPage = 1;
		me.getKeywordQueryData().getStore().reload();
    },
	
	//编辑页面下的提交关键字按钮操作
	onSubmitKeywordClick: function(){
    	var me = this;
    	var win = me.getKeywordEast();
    	var former = me.getKeywordEdit();
    	var value = former.getValues();
    	value.keywordItem = value.keywordItem.replace(/^\s+|\s+$/g,"");
    	if(value.keywordItem == ''){
    		Ext.Msg.alert("提示", '请输入关键字');
    		return false;
    	}
    	Ext.Ajax.request({
			url:'keyword/saveKeyword.do',
			params: value,
			success : function(respond, opts) {
				var responds = Ext.decode(respond.responseText);
				if(responds.failure){
					Ext.Msg.alert("提示", responds.msg);
					return false;
				}
				win.hide();
				me.getKeywordList().getStore().reload();
			},
			failure : function(respond, action) {
	           Ext.Msg.alert("提示", action.result.msg ? action.result.msg : "网络错误");
			}
		});
    },
	
	//编辑页面下的提交文件按钮操作
	onSubmitFileClick: function(){
    	var me = this;
    	var win = me.getKeywordEast();
    	var former = me.getKeywordEdit();
    	former.getForm().submit({
			url:'keyword/batchSaveKeyword.do',
			method : 'post',
			enctype : "multipart/form-data",
			waitMsg: '上传中...',
			success : function(respond, opts) {
				var responds = opts.result;
				if(responds.failure){
					Ext.Msg.alert("提示", responds.msg);
					return false;
				}
				win.hide();
				me.getKeywordList().getStore().reload();
			},
			failure : function(respond, action) {
	            Ext.Msg.alert("提示", action.result.msg ? action.result.msg : "网络错误");
			}
		});
    },
	
	onMinToolClick: function(){
    	var win = this.getKeywordEast();
    	win.hide();
    },
    
    onBeforeRender: function(component,options){
    	wmf.data.loadAction(this.getKeywordMain().itemId, this.getKeywordList());
    	wmf.keyword.params = {};
    	component.getStore().load();
    }
});
